﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
namespace TinyCinema.Data
{
    public class Department : Execution
    {
        public string ID { get; set; }
        public string Name { get; set; }
        public ObservableCollection<Employee> Employees { get; set; }
        public Department()
        {
            ID = IDGenerate(Tables.Department);
        }
        public Department(int index)
        {
            Index = index;
        }
        public Department(string _ID)
        {
            ID = _ID;
            PutTableIntoDataSet(Tables.Department);
            var tbl = ds.Tables[Tables.Department];
            var Row = tbl.Select(DepartmentTable.ID + "='" + ID + "'")[0];
            Name = Row[DepartmentTable.Name] as string;

            if (lstDepartment == null)
            {
                lstDepartment = new ObservableCollection<Department>();
                lstDepartment = GetAllDepartments();
            }
            for (int i = 0; i < lstDepartment.Count; ++i)
                if (ID == lstDepartment[i].ID) Index = i;
        }
       
        public static ObservableCollection<Department> lstDepartment { get; set; }
        int _Index { get; set; }
        public int Index
        {
            get { return _Index; }
            set
            {
                ID = lstDepartment[value].ID;
                Name = lstDepartment[value].Name;
                _Index = value;
            }
        }
        public static ObservableCollection<Department> GetAllDepartments()
        {
            var lst = new ObservableCollection<Department>();
            PutTableIntoDataSet(Tables.Department);
            var tbl = ds.Tables[Tables.Department];
            var Rows = tbl.Select();
            lst = new ObservableCollection<Department>();
            foreach (var row in Rows)
                lst.Add(new Department(row[DepartmentTable.ID] as String));
            return lst;
        }
        public void Delete()
        {
            var tbl = ds.Tables[Tables.Department];
            var Row = tbl.Select(DepartmentTable.ID + "='" + ID + "'")[0];
            Row.Delete();
            SaveCommand(Tables.Department);
        }
        public void Save()
        {
            var tbl = ds.Tables[Tables.Department];
            var Rows = tbl.Select(DepartmentTable.ID + "='" + ID + "'");
            if (Rows.Length == 0) //đang tạo mới
            {
                var Row = tbl.NewRow();
                Row[DepartmentTable.ID] = ID;
                Row[DepartmentTable.Name] = Name;
                tbl.Rows.Add(Row);
                SaveCommand(Tables.Department);
            }
            else
            {
                var Row = Rows[0];
                Row[DepartmentTable.Name] = Name;
                SaveCommand(Tables.Department);
            }
            lstDepartment = GetAllDepartments();
            OnPropertyChanged("lstDepartment");
        }
    }
}
